<?php 
include 'config.php';
if (!defined('BASEPATH')) exit('No direct script access allowed');
include 'lib/fun.php';
include 'lib/md.php';

if ( isset($_SERVER['PATH_INFO'])) {
	$path_info = $_SERVER['PATH_INFO'];
}else if(isset($_SERVER['ORIG_PATH_INFO'])){
	$path_info = $_SERVER['ORIG_PATH_INFO'];
}

$url = rstrtrim($path_info,'/index.php');

// 支持md中相对png
if (file_exists(MDPATH.$url) and is_file(MDPATH.$url)) {
	$info = pathinfo(MDPATH.$url);
	if($info["extension"] = "png"){
		header("Content-Type:image/png");
		readfile(MDPATH.$url);
		exit;
	}
}

if (file_exists(MDPATH.$url.'.md') and isset($_GET['s']) ) {
	header("Content-Type:text/text");
	readfile(MDPATH.$url.'.md');
	exit;
}


if (!$url) {
    // $md = MDPATH.'/index.md';
    header('location:'.GLOBAL_URL.'/index');
}else{
	$md = MDPATH.$url.'.md';
}

$content  = false;
$path_arr = explode('/',$url);

if (!file_exists($md)) {
	if (end($path_arr) != 'index' and file_exists(MDPATH.$url)) {
		$mdindex = MDPATH.$url.'/index.md';
		if (file_exists($mdindex)) {
			header('location:'.GLOBAL_URL.$url.'/index');
		}else{
			$list = get_filename(MDPATH.$url);
			$content = "<h1>$url</h1><ul>";
			if (array_key_exists('dirs',$list)) {
				foreach ($list['dirs'] as $d => $p){
					$content .="<li><a href='$url/$d/'>$d</a></li>"; 
				}
				$title = '';
			}
			if (array_key_exists('files',$list)) {
				foreach ($list['files'] as $f => $p){
					if(pathinfo($p, PATHINFO_EXTENSION) == 'md'){
						$file = fopen($p,"r");
						$title= str_replace("#",'',trim(fgets($file)));
						fclose($file);
						$content .="<li><a href='$url/$f'>$title</a></li>"; 
					}
				}
			}
			$content .= '</ul>';
			$path       = _path($path_arr);
			$menu       = isset($path_arr[1]) ? $path_arr[1] : '';
			$date       = date('Y-m-d');  // to read file info
			$tags = '';
			$info = '';
			include 'views/markdown.php';
			return 0;
		}
	}
	include 'views/404.php';
	exit;
}else{
	// 记录指纹
	$content = Markdown(file_get_contents($md));
}

$md_menu_file = rstrtrim($md,end($path_arr).'.md').'menu.md';
if (file_exists($md_menu_file)) {
	$md_menu = Markdown(file_get_contents($md_menu_file));
}

$path       = _path($path_arr);
$menu       = isset($path_arr[1]) ? $path_arr[1] : '';
$date       = date('Y-m-d');  // to read file info
$file       = fopen($md,"r");
$title_real = fgets($file);
$title      = str_replace("**title:**",'',trim(fgets($file))); // filename 取出 seo
$tags       = str_replace("**tags:**",'',trim(fgets($file)));
$info       = str_replace("**info:**",'',trim(fgets($file)));
$title or $title = trim($title_real,'(#|_)');
fclose($file);

/*
 * _path($path_info)
 * return array (title => path)
*/
function _path($p){
	$r = array();
	foreach($p as $k => $v){
		$r[$k]['title'] = $v ? $v :'&#64; Index';
		if (isset($p[$k-1])) {
			$r[$k]['path'] = $r[$k-1]['path'].'/'.$v;
		}else{
			$r[$k]['path']  = $v;
		}
	}
	return $r;
}

/*
function _scan($dir=false){
	$dir =  $dir ? $dir :MDPATH;
	if ($dir && is_dir($dir)) {
		$f = scandir($dir);
	}
}
*/

if (isset($_GET['h']) ) {
	$str = '
	<!DOCTYPE html>
	<html lang="zh">
	<head>
		<meta charset="utf-8">
		<title>'.$title.'</title>
		<meta name="keywords" content="'.$tags.'"/>
		<meta name="generator" content="vim" />
		<meta name="copyright" content="2012" />
		<meta name="distribution" content="'.$info.'" />
		<meta http-equiv="content-language" content="zh-CN" />
	</head>
	<body>
		'.$content.'
	</body>
	</html>
	';
	echo $str;
	$file = end($path_arr);
	file_put_contents('./cache/'.$file.'.html',$str);
}else{
	include 'views/markdown.php';
}


?>

